package com.wf.ew.sell.service.impl;

import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.ArithUtil;
import com.wf.ew.sell.model.EntryBillSon;
import com.wf.ew.storage.biz.ProductStorageBiz;
import com.wf.ew.storage.model.ProductStorage;
import com.wf.ew.system.model.User;
import com.wf.ew.sell.biz.OutBillSonBiz;
import com.wf.ew.sell.model.OutBillSon;
import com.wf.ew.sell.service.OutBillSonService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;

/**
 *
 * 数据服务层接口实现类
 * @author : Doman
 * @date : 2019-12-06 17:43:45
 * @version ：1.0
 */
@Service
public class OutBillSonServiceImpl implements OutBillSonService {

	@Autowired
	private OutBillSonBiz outBillSonBiz;
	@Autowired
	private ProductStorageBiz productStorageBiz;

	@Override
	public String list(PageResult<T> page, OutBillSon outBillSon, User loginUser) {
		return outBillSonBiz.list(page,outBillSon,loginUser);
	}
	
	@Override
	public List<OutBillSon> list(OutBillSon outBillSon, User loginUser) {
		return outBillSonBiz.list(outBillSon,loginUser);
	}

	@Override
	public OutBillSon getById(OutBillSon outBillSon) {
		return outBillSonBiz.getById(outBillSon);
	}

	@Transactional
	@Override
	public JsonResult add(OutBillSon outBillSon, User loginUser) {
		if (outBillSonBiz.add(outBillSon, loginUser)) {
			if(outBillSon.getNum()>=0){
				ProductStorage ps = new ProductStorage();
				ps.setWarehouseName(outBillSon.getWarehouseName());
				ps.setProductName(outBillSon.getProductName());
				ps.setSpecification(outBillSon.getSpecification());
				ps.setLengths(outBillSon.getLengths());
				ps.setNegative(outBillSon.getNegative());
				ps.setNum(outBillSon.getNum());
				ps.setLiWeight(outBillSon.getLiWeight());
				ps.setLiTotalWeight(Double.valueOf(outBillSon.getLiTotalWeight()));
				ps.setPrice(outBillSon.getPrice());
				ps.setModel(outBillSon.getModel());
				ps.setLocality(outBillSon.getLocality());
				List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
				if (psList != null && psList.size()>0) {
					ProductStorage ps2 = psList.get(0);
					ps2.setNum(ArithUtil.sub(0,outBillSon.getNum()));
					ps2.setLiTotalWeight(ArithUtil.sub(0,Double.valueOf(outBillSon.getLiTotalWeight())));
					productStorageBiz.updateNum(ps2, loginUser);
				} else {
					productStorageBiz.add(ps, loginUser);
				}
			}
			return JsonResult.ok("添加成功");
		}
		return JsonResult.error("添加失败");
	}

	@Transactional
	@Override
	public JsonResult update(OutBillSon outBillSon, User loginUser) {
		OutBillSon b2 = outBillSonBiz.getById(outBillSon);
		if (outBillSonBiz.update(outBillSon, loginUser)) {
			if(outBillSon.getNum()>=0){
				ProductStorage ps = new ProductStorage();
				ps.setWarehouseName(outBillSon.getWarehouseName());
				ps.setProductName(outBillSon.getProductName());
				ps.setSpecification(outBillSon.getSpecification());
				ps.setLengths(outBillSon.getLengths());
				ps.setNegative(outBillSon.getNegative());
				List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
				if (psList != null && psList.size()>0) {
					ProductStorage ps2 = psList.get(0);
					ps2.setNum(ArithUtil.sub(0,ArithUtil.sub(outBillSon.getNum(),b2.getNum())));
					ps2.setLiTotalWeight(ArithUtil.sub(0,
							ArithUtil.sub(Double.valueOf(outBillSon.getLiTotalWeight()),
									Double.valueOf(b2.getLiTotalWeight()))));
					productStorageBiz.updateNum(ps2, loginUser);
				} else {
					productStorageBiz.add(ps, loginUser);
				}
			}
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Transactional
	@Override
	public JsonResult delete(OutBillSon outBillSon, User loginUser) {
		outBillSon.setId(outBillSon.getIds());
		OutBillSon b2 = outBillSonBiz.getById(outBillSon);
		if (outBillSonBiz.delete(outBillSon, loginUser)) {
			ProductStorage ps = new ProductStorage();
			ps.setWarehouseName(b2.getWarehouseName());
			ps.setProductName(b2.getProductName());
			ps.setSpecification(b2.getSpecification());
			ps.setLengths(b2.getLengths());
			List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
			if (psList != null && psList.size()>0) {
				ProductStorage ps2 = psList.get(0);
				ps2.setNum(b2.getNum());
				ps2.setLiTotalWeight(Double.valueOf(b2.getLiTotalWeight()));
				productStorageBiz.updateNum(ps2, loginUser);
			}
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}
}
